package com.example.threemoni.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

/**
 * @Author 宋伟宁
 * @Date 2023/10/25
 * @Version 1.0
 **/
@EnableWebMvc
@Configuration
@ComponentScan("com.example.threemoni")
@MapperScan("com.example.threemoni.mapper")
@PropertySource("classpath:jdbc.properties")
public class MvcConfiguration {

    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driver}")
    private String driver;

    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(this.url);
        ds.setUsername(this.username);
        ds.setDriverClassName(this.driver);
        ds.setPassword(this.password);
        return  ds;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(){
         SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
         sessionFactoryBean.setDataSource(dataSource());
         sessionFactoryBean.setPlugins(new PageInterceptor());
         return  sessionFactoryBean;
    }
}
